﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;

using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using AutoMapper;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class SystemConfigController : EntityController<SystemConfig, SystemConfigSearch, ISystemConfigViewModel, ISystemConfigAppService>, ISystemConfigController
    {
        public override string ControllerName
        {
            get { return ControllerConst.SYSTEMCONFIG; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public SystemConfigController()
        {
            this.Model = ModelFactory.CreateModel<ISystemConfigViewModel>();
        }


        [ActionName(ActionConst.INDEX)]
        public ActionResult Index()
        {
            return View();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "sysconfig_query")]
        public ActionResult AddSystemConfig()
        {
            SystemConfigJsonViewModel json = new SystemConfigJsonViewModel();
            json.rows = new List<SystemConfigJson>();

            IList<SystemConfig> systemConfigs = this.AppService.GetBySearch(SearchFactory.CreateSystemConfigSearch()).ToList();
            foreach (SystemConfig sc in systemConfigs)
            {
                SystemConfigJson scj = Mapper.Map<SystemConfig, SystemConfigJson>(sc);

                json.rows.Add(scj);
            }
            json.total = systemConfigs.Count;
            this.Model.SystemConfigs = json;

            return View("~/Views/System/v1/addSystemConfig.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "sysconfig_edit")]
        public JsonResult AddSystemConfig(string updated)
        {
            BoolGenericResult<SystemConfig> result = null;
            List<SystemConfigJson> systemConfigs = JsonConvert.DeserializeObject<List<SystemConfigJson>>(updated);

            foreach (SystemConfigJson json in systemConfigs)
            {
                SystemConfig sc = new SystemConfig();
                sc.Id = json.ID;
                sc.Type = (SystemIdentifier.SystemConfigType)json.Type;
                sc.Item = json.Item;
                sc.Value = json.Value;
                sc.Remark = json.Remark;
                result = this.AppService.Edit(sc);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
